package com.ruoyi.web.qualityController;

import com.google.common.net.HttpHeaders;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.entity.Upload;
import com.ruoyi.system.domain.quality.QualityFinishedExamine;
import com.ruoyi.system.domain.quality.QualityIncomingMaterial;
import com.ruoyi.system.domain.quality.QualityInspectionItem;
import com.ruoyi.system.service.IQualityFinishedExamineService;
import com.ruoyi.system.service.IUploadService;
import com.ruoyi.web.controller.common.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/2/24 14:06
 * 成品检验单
 */
@Api(tags = "成品检验单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/quality/finishedExamine")
public class QualityFinishedExamineController extends BaseController {


    @Autowired
    IQualityFinishedExamineService iQualityFinishedExamineService;

    /**
     * 成品检验单查询列表
     *
     * @param quality
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('system:finishedExamine:list')")
    @ApiOperation(value = "查询成品检验单列表")
    @PostMapping("/list")
    public TableDataInfo list(QualityFinishedExamine quality) {
        startPage();
        List<QualityFinishedExamine> list = iQualityFinishedExamineService.selectQualityFinishedExamineList(quality);
        return getDataTable(list);
    }

    /**
     * 根据id成品检验单详情
     */
    //@PreAuthorize("@ss.hasPermi('system:finishedExamine:query')")
    /* @ApiOperation(value = "获取成品检验单详情")*/
    @GetMapping("/{id}")
    public QualityFinishedExamine getInfo(@PathVariable("id") Long id) {
        return iQualityFinishedExamineService.selectSetAreaById(id);

    }


    @ApiOperation("删除成品检验单")
    @DeleteMapping("delete/id")
    public AjaxResult deleteList(Long id) {
        iQualityFinishedExamineService.deleteQualityFinishedExamineById(id);
        return AjaxResult.success("删除成功！");
    }

    /**
     * 新增成品检验单
     */
    //@PreAuthorize("@ss.hasPermi('stores:problemFeedback:add')")
    @Log(title = "成品检验单", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增成品检验单")
    @PostMapping("/add")
    public AjaxResult add(@ApiParam(value = "新增成品检验单实体类", required = true) @RequestBody QualityFinishedExamine quality) {
        iQualityFinishedExamineService.insertQualityFinishedExamine(quality);
        return AjaxResult.success("添加成功");
    }


    @Autowired
    private com.ruoyi.web.controller.Upload.UploadController.UploadService uploadService;

    @Autowired
    IUploadService iUploadService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        // 调用 Service 层的方法保存图片
        if (file.getSize() < 0 || file.getSize() == 0) {
            throw new FileNotFoundException("没有文件");
        }
        String url = uploadService.save(file);
        QualityFinishedExamine qualityIncomingMaterial = new QualityFinishedExamine();
        Upload upload = new Upload();
        upload.setMateId(qualityIncomingMaterial.getId());
        upload.setUrl(url);
        iUploadService.insertUpload(upload);
        // 返回图片 URL
        return url;
    }

    @ApiOperation("下载")
    @GetMapping("/download/image")
    public void downloadImage(@RequestParam("path") String imagePath, HttpServletResponse response) throws IOException {
        final File file = new File(imagePath);
        if (file.exists()) {
//            String contentType = URLConnection.guessContentTypeFromName(file.getName());
            final String suffix = imagePath.substring(imagePath.lastIndexOf(".") + 1);
            final String contentType = URLConnection.getFileNameMap().getContentTypeFor(".".concat(suffix));
//            response.setContentType(contentType);
            response.setContentType(contentType.concat(";charset=UTF-8"));
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
            response.setContentLength((int) file.length());
            try (final InputStream inputStream = Files.newInputStream(file.toPath())) {
                FileCopyUtils.copy(inputStream, response.getOutputStream());
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }


    /*  *//**
     * 新增成品检验单
     *//*
    //@PreAuthorize("@ss.hasPermi('stores:problemFeedback:add')")
    @Log(title = "成品检验单", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增成品检验单")
    @PostMapping("/add")
    public AjaxResult add(@ApiParam(value = "新增成品检验单实体类", required = true) @RequestBody QualityInspectionItem quality) {
        iQualityFinishedExamineService.insertQuality(quality);
        return AjaxResult.success("添加成功");
    }
*/

}



